package com.spark.rectify

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.alert
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import kotlinx.android.synthetic.main.frag_rect_history.*

/**
 * 纠违历史
 */
class RectHistoryFragment : BaseFragment() {
    private var currPage = 1
    private var totalPage = 1
    private lateinit var rectHistoryAdapter: RectHistoryAdapter

    companion object {
        fun newInstance(): RectHistoryFragment {
            val target = RectHistoryFragment()
            val args = Bundle()

            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_rect_history
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        iv_category.setOnClickListener {
            val dfRectManage = DFRectManage.newInstance()
            dfRectManage.showNow(childFragmentManager, "DFRectManage")
            dfRectManage.addFilterListener {
                LogUtils.e(it)
            }
        }
        fetchHistories()
        refresh_layout.apply {
            setOnRefreshListener {
                currPage = 1
                fetchHistories()
            }
            setOnLoadmoreListener {
                if (currPage > totalPage) {
                    finishLoadmore()
                    alert("没有更多数据了")
                } else {
                    currPage++
                    fetchHistories(page = currPage)
                }
            }
        }
        recy_history.apply {
            rectHistoryAdapter = RectHistoryAdapter(R.layout.item_rect_history).apply {
                setOnItemClickListener { adapter, view, position ->

                }
            }
            adapter = rectHistoryAdapter
            layoutManager = LinearLayoutManager(context)
        }
    }

    @SuppressLint("CheckResult")
    private fun fetchHistories(seachstr: String = "", page: Int = 1, state: Int = -1, begindate: String = "", enddate: String = "") {
        val params = mutableMapOf("pageSize" to "10", "page" to "$page")
        if (!seachstr.isNullOrBlank()) params["searchstr"] = seachstr
        if (state != -1) params["state"] = "$state"
        if (!begindate.isNullOrBlank()) params["begindate"] = begindate
        if (!enddate.isNullOrBlank()) params["enddate"] = enddate

        Http.request()
                .rxPost("${Const.base_url}${Const.act_rectify_history_list}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {

                        },
                        {
                            it.printStackTrace()
                            report(it)
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            dispose = it
                        }
                )
    }
}

data class RectHistoryModel(
        var id: String = "",
        var name: String = "",
        var state: String = "",
        var unit: String = "",
        var leader: String = "",
        var time: String = "",
        var content: String = ""
)

class RectHistoryAdapter(layoutRes: Int) : BaseQuickAdapter<RectHistoryModel, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: RectHistoryModel?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_name, item.name)
            setText(R.id.tv_state, item.state)
            setText(R.id.tv_unit, item.unit)
            setText(R.id.tv_leader, item.leader)
            setText(R.id.tv_time, item.time)
            setText(R.id.tv_content, item.content)
        }
    }

}